#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年7月10日

@author: chenyongbing
'''
import sys,os,re
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

out = '''
2015-07-02 12:46:15 DeviceServerHandler 116 [nioEventLoopGroup-4-2] INFO : acct,048d3861dd30,buf={ "device_upflow": 157455511, "device_downflow": -1275402679, "device_up_rate": 24841, "device_down_rate": 237364, "ntp_status": 1, "version": "V1.3.141231\n_2.1.5_20150602", "users": [ { "sta_name": "", "ssid": "5Space-3", "sta_ip": "192.168.8.108", "sta_mac": "b8098a5e41a2", "sta_auth_type": 1, "sta_mode": 2, "sta_uptime": 481, "sta_up_rate": 0, "sta_down_rate": 0, "sta_upflow": 122277, "sta_downflow": 825289 }, { "sta_name": "hsh", "ssid": "5Space-3", "sta_ip": "192.168.8.106", "sta_mac": "3c46d814608b", "sta_auth_type": 1, "sta_mode": 2, "sta_uptime": 9558, "sta_up_rate": 20372, "sta_down_rate": 1172, "sta_upflow": 65481679, "sta_downflow": 253561828 }, { "sta_name": "MI3-xiaomishouj", "ssid": "5Space-3", "sta_ip": "192.168.8.103", "sta_mac": "68dfdd92943f", "sta_auth_type": 1, "sta_mode": 2, "sta_uptime": 9566, "sta_up_rate": 4267, "sta_down_rate": 230178, "sta_upflow": 50269379, "sta_downflow": -1844810206 } ] }
2015-07-02 12:51:18 DeviceServerHandler 116 [nioEventLoopGroup-4-2] INFO : acct,048d3861dd30,buf={ "device_upflow": 10716526, "device_downflow": -1223603479, "device_up_rate": 3737, "device_down_rate": 125576, "ntp_status": 1, "version": "V1.3.141231\n_2.1.5_20150602", "users": [ { "sta_name": "hsh", "ssid": "5Space-3", "sta_ip": "192.168.8.106", "sta_mac": "3c46d814608b", "sta_auth_type": 1, "sta_mode": 2, "sta_uptime": 9861, "sta_up_rate": 812, "sta_down_rate": 0, "sta_upflow": 67622938, "sta_downflow": 253692512 }, { "sta_name": "MI3-xiaomishouj", "ssid": "5Space-3", "sta_ip": "192.168.8.103", "sta_mac": "68dfdd92943f", "sta_auth_type": 1, "sta_mode": 2, "sta_uptime": 9869, "sta_up_rate": 3625, "sta_down_rate": 125514, "sta_upflow": 51359338, "sta_downflow": -1793271490 } ] }
2015-07-02 12:51:18 DeviceService 434 [pool-1-thread-948] INFO : 未收到acct消息，暂不往设备上发送踢下线请求，apmac=048d3861dd30，stamac=b8098a5e41a2，lastAliveTime=2015-07-02 12:38:14,now=2015-07-02 12:51:18
2015-07-02 12:56:20 DeviceServerHandler 116 [nioEventLoopGroup-4-2] INFO : acct,048d3861dd30,buf={ "device_upflow": 162384374, "device_downflow": -1172141745, "device_up_rate": 15139, "device_down_rate": 299677, "ntp_status": 1, "version": "V1.3.141231\n_2.1.5_20150602", "users": [ { "sta_name": "hsh", "ssid": "5Space-3", "sta_ip": "192.168.8.106", "sta_mac": "3c46d814608b", "sta_auth_type": 1, "sta_mode": 2, "sta_uptime": 10164, "sta_up_rate": 580, "sta_down_rate": 0, "sta_upflow": 67743149, "sta_downflow": 253732206 }, { "sta_name": "MI3-xiaomishouj", "ssid": "5Space-3", "sta_ip": "192.168.8.103", "sta_mac": "68dfdd92943f", "sta_auth_type": 1, "sta_mode": 2, "sta_uptime": 10171, "sta_up_rate": 13849, "sta_down_rate": 266429, "sta_upflow": 52875925, "sta_downflow": -1741980449 } ] }

'''




if len(sys.argv)==1:
    print u'请输入日志文件'
    sys.exit(1)
else:
    logfile = sys.argv[1]
    
    out = open(logfile).read()

datas = {}

out = re.sub('\n_','_',out)

for ltime,device , buf in re.findall('([\d]{4}-[\d]{2}-[\d]{2} [\d]{2}:[\d]{2}:[\d]{2}) DeviceServerHandler.*?acct,([^\,]+),buf=(.*)',out):
    #print ltime,device,buf
    try:
        bufDict = eval(buf)
    except:continue
    if bufDict.has_key('device_upflow'):
        device_upflow = bufDict['device_upflow']
    else:
        device_upflow = None
    if bufDict.has_key('device_downflow'):
        device_downflow = bufDict['device_downflow']
    else:
        device_downflow = None
        
    if bufDict.has_key('version'):
        version = bufDict['version']
    else:
        version = 'unknown'

    if not datas.has_key(device):
        datas[device] = [device_upflow,device_downflow]
        continue
    else:
        last_device_upflow,last_device_downflow = datas[device]
    msg = ''
    if last_device_upflow !=None and device_upflow!=None and int(device_upflow) < int(last_device_upflow) and len(str(device_upflow)) == len(str(last_device_upflow)):
         msg+='upflow:%s>%s\t'%(last_device_upflow,device_upflow)
   
    
    if last_device_downflow !=None and device_downflow!=None and int(device_downflow) < int(last_device_downflow) and len(str(device_downflow)) == len(str(last_device_downflow)):
         msg+='downflow:%s>%s'%(last_device_downflow,device_downflow)
    
    if len(msg)!=0:
        msg= '\t'.join([ltime,device,version,msg])
        print msg
    datas[device] = [device_upflow,device_downflow]